import bean.Person;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import org.junit.Test;

import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ObjectSerialize {
    @Test
    public void objectSerialize(){
        // 序列化
        // 默认如果有控制则忽略值为空的字段
        Person person = new Person("张三", 18, null);
        Gson gson = new Gson();
        String s = gson.toJson(person);
        System.out.println(s);
        // 将空值的字段也打印出来保留
        Gson gson1 = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
        String s1 = gson1.toJson(person);
        System.out.println(s1);
        
        // 反序列化
        System.out.println("---------------------反序列化--------------------------");
        Person person1 = gson.fromJson(s, Person.class);
        System.out.println(person1.toString());
        Person person2 = gson.fromJson(s1, Person.class);
        System.out.println(person2.toString());

    }

    /**
     * JsonObject 测试
     */
    @Test
    public void createJsonObjectTest(){
        Gson gson = new Gson();
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("code", 400);
        jsonObject.addProperty("message", "参数错误");
        String toJson = gson.toJson(jsonObject);
        String exceptedJson = "{\"code\":400,\"message\":\"参数错误\"}";
        System.out.println(toJson.equals(exceptedJson));
    }
    /**
     * json字符串转map
     */
    @Test
    public void testMap(){
            String jsonString = "{'employee.name':'one','employee.salary':10}";
            Gson gson = new Gson();
        JsonElement jsonElement = gson.fromJson(jsonString, JsonElement.class);
        System.out.println(jsonElement.toString());

        Map<String,Object> map = gson.fromJson(jsonString, Map.class);
            Set<Map.Entry<String, Object>> entries = map.entrySet();
            for (Map.Entry<String,Object> entry:entries){
                System.out.println(entry.getKey()+"="+entry.getValue());
            }
    }

    /**
     * jsonParser
     */
    @Test
    public void  jsonParserTest(){

        JsonParser jsonParser = new JsonParser();
        jsonParser.parse(new Reader() {
            @Override
            public int read(char[] cbuf, int off, int len) throws IOException {
                return 0;
            }

            @Override
            public void close() throws IOException {

            }
        });
    }
    @Test
    public void testSerializeCollection(){

        Person p1 = new Person("张胜男1", 18, null);
        Person p2 = new Person("张胜男2", 28, null);
        Person p3 = new Person("张胜男3", 38, null);
        Person p4 = new Person("张胜男4", 48, null);
        List<Person> list = new ArrayList<Person>();
        list.add(p1);
        list.add(p2);
        list.add(p3);
        list.add(p4);
// 将字符串序列化为对象数组
        Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().create();
        String s = gson.toJson(list);
        System.out.println(s);
        Person[] people = gson.fromJson(s, Person[].class);
        System.out.println(people[0] instanceof Person);
// 通过type 将json字符串反序列为指定对象的集合
        Type type = new TypeToken<List<Person>>() {
        }.getType();
        List<Person> plist = gson.fromJson(s, type);
        System.out.println(plist.get(0) instanceof  Person);
    }
}
